var use="";		//锟斤拷锟斤拷锟斤拷途
var taste="";	//锟斤拷味
var size="";	//锟斤拷小
var level="";	//锟斤拷锟斤拷


//锟酵伙拷锟斤拷陆
function login()
{
	var username=$("#username").val();
	var password=$("#password").val();
	
	if(username=='')
	{
		alert("username can't be empty");
		return;
	}
	if(password=='')
	{
		alert("password can't be empty");
		return;
	}
	//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷login锟斤拷
	$.post("UserServlet",{action:"login",name:username,pwd:password},function(data){
		//锟斤拷锟�锟截斤拷锟�
		if(data=='false')
		{
			alert("username or password wrong");
		}
		if(data=='true')
		{
			toURL("index.jsp");
		}
	});
}
//锟斤拷锟斤拷员锟斤拷陆
function adminLogin()
{
	var username=$("#username").val();
	var password=$("#password").val();
	
	if(username=='')
	{
		alert("username can't be empty");
		return;
	}
	if(password=='')
	{
		alert("password can't be empty");
		return;
	}
	//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷adminLogin锟斤拷
	$.post("UserServlet",{action:"adminLogin",name:username,pwd:password},function(data){
		if(data=='false')
		{
			alert("username or password wrong");
		}
		if(data=='true')
		{
			toURL("orders.jsp");
		}
	});
}
//锟酵伙拷注锟斤拷
function regist()
{
	var username=$("#username").val();
	var password=$("#password").val();
	var confirm=$("#confirm").val();
	var email=$("#idcard").val();
	
	clearContent("#username_tip");
	clearContent("#password_tip");
	clearContent("#confirm_tip");
	clearContent("#idcard_tip");
	
	if(username=='')
	{
		showTip("username_tip","* username can't be empty");
		return;
	}
	if(password=='')
	{
		showTip("password_tip","* password can't be empty");
		return;
	}
	
	if(password!=confirm)
	{
		showTip("confirm_tip","* confirm password should be equal to password");
		return;
	}
	if(!isValidEmail(email))
	{
		showTip("idcard_tip","* email is incorrect");
		return;
	}
	//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷regist锟斤拷
	$.post("UserServlet",{action:"regist",name:username,pwd:password,idcard:email},function(data){
		if(data=='false')
		{
			alert("username or password wrong");
		}
		else if(data=='true')
		{
			toURL("index.jsp");
		}
		else
		{
			alert(data);
		}
		
	});
}
//锟酵伙拷锟剿筹拷
function quit()
{
	//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷logout锟斤拷
	$.post("UserServlet",{action:"logout"},function(){
			toURL("index.jsp");
	});
}
//锟斤拷锟斤拷员锟剿筹拷
function adminQuit()
{
	//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷adminLogout锟斤拷
	$.post("UserServlet",{action:"adminLogout"},function(){
			toURL("admin.jsp");
	});
}
//锟酵伙拷锟铰碉拷
function order()
{
	var cakeId = getUrlParaValue(location.href,"cake");
	var cartId = getUrlParaValue(location.href,"cart");
	var address=$("#address").val();
	var phone=$("#phone").val();
	var quantity=$("#quantity").val();
	var remark=$("#remark").val();
	
	clearContent("#address_tip");
	clearContent("#phone_tip");
	clearContent("#quantity_tip");
	clearContent("#remark_tip");
	
	if(address=='')
	{
		showTip("address_tip","* address can't be empty");
		return;
	}
	
	if(!isMobile(phone))
	{
		showTip("phone_tip","* phone number should be 11 digital characters");
		return;
	}
	
	if(!isNum(quantity))
	{
		showTip("quantity_tip","* quantity should be positive number");
		return;
	}
	//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷order锟斤拷
	$.post("UserServlet",{
		action:"order",
		cake:cakeId,
		cart:cartId,
		address:address,
		phone:phone,
		remark:remark,
		quantity:quantity
	},function(data){
		if(data=='false')
		{
			alert("order failed");
		}
		if(data=='true')
		{
			alert("order sucess,your cake will be send in 2 hours");
			toURL("myOrder.jsp");
		}
	});
}
//锟斤拷询锟斤拷品
function search()
{
	var keywords=$("#keywords").val();
	if(keywords!='')
	{
		toURL('cakes.jsp?keywords='+keywords);
	}
}
//锟酵伙拷取锟斤拷
function cancelOrder(orderId)
{
	if(confirm("do you really want to cancel the order?"))
	{
	if(orderId)
	{
		//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷cancel锟斤拷
		$.post("UserServlet",{action:"cancel",order:orderId},function(data){
			if(data=='false')
			{
				alert("cancel failed");
			}
			else if(data=='true')
			{
				toURL("myOrder.jsp");
			}
			else if(data=='true2')
			{
				toURL("orders.jsp");
			}
		});
	}
	}
}
//锟酵伙拷取锟斤拷DIY锟斤拷锟斤拷
function cancelDiyOrder(orderId)
{
	if(confirm("do you really want to cancel the diy order?"))
	{
	if(orderId)
	{
		//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷cancel锟斤拷
		$.post("UserServlet",{action:"cancelDiy",order:orderId},function(data){
			if(data=='false')
			{
				alert("cancel failed");
			}
			else if(data=='true')
			{
				toURL("myOrder.jsp");
			}
			else if(data=='true2')
			{
				toURL("orders.jsp");
			}
		});
	}
	}
}
//锟斤拷锟斤拷员取锟斤拷
function cancelOrderByAdmin(orderId)
{
	if(confirm("do you really want to cancel the order?"))
	{
	if(orderId)
	{
		//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷adminCancel锟斤拷
		$.post("UserServlet",{action:"adminCancel",order:orderId},function(data){
			if(data=='false')
			{
				alert("cancel failed");
			}
			if(data=='true')
			{
				toURL("orders.jsp");
			}
		});
	}
	}
}
//锟斤拷锟斤拷员取锟斤拷DIY锟斤拷锟斤拷
function cancelDiyOrderByAdmin(orderId)
{
	if(confirm("do you really want to cancel the diy order?"))
	{
	if(orderId)
	{
		//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷adminCancel锟斤拷
		$.post("UserServlet",{action:"adminCancelDiy",order:orderId},function(data){
			if(data=='false')
			{
				alert("cancel failed");
			}
			if(data=='true')
			{
				toURL("orders.jsp");
			}
		});
	}
	}
}
//锟斤拷锟斤拷员锟斤拷锟斤拷
function sendOrder(orderId)
{
	if(confirm("do you really want to send the order?"))
	{
		if(orderId)
		{
			//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷send锟斤拷
			$.post("UserServlet",{action:"send",order:orderId},function(data){
				if(data=='false')
				{
					alert("send failed");
				}
				if(data=='true')
				{
					toURL("orders.jsp");
				}
			});
		}
	}
}
//锟斤拷锟斤拷员锟斤拷锟斤拷DIY
function sendDiyOrder(orderId)
{
	if(confirm("do you really want to send the diy order?"))
	{
		if(orderId)
		{
			//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷send锟斤拷
			$.post("UserServlet",{action:"sendDiy",order:orderId},function(data){
				if(data=='false')
				{
					alert("send failed");
				}
				if(data=='true')
				{
					toURL("orders.jsp");
				}
			});
		}
	}
}
//锟酵伙拷确锟斤拷锟秸伙拷
function confirmOrder(orderId)
{
	if(confirm("do you really want to confirm the order?"))
	{
		if(orderId)
		{
			//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷confirm锟斤拷
			$.post("UserServlet",{action:"confirm",order:orderId},function(data){
				if(data=='false')
				{
					alert("confirm failed");
				}
				if(data=='true')
				{
					toURL("myOrder.jsp");
				}
			});
		}
	}
}
//锟酵伙拷确锟斤拷DIY锟斤拷锟斤拷锟秸伙拷
function confirmDiyOrder(orderId)
{
	if(confirm("do you really want to confirm the diy order?"))
	{
		if(orderId)
		{
			//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷confirm锟斤拷
			$.post("UserServlet",{action:"confirmDiy",order:orderId},function(data){
				if(data=='false')
				{
					alert("confirm failed");
				}
				if(data=='true')
				{
					toURL("myOrder.jsp");
				}
			});
		}
	}
}
//锟酵伙拷锟睫改讹拷锟斤拷
function updateOrder()
{
	//锟斤拷取url锟叫碉拷orderid锟斤拷值
	var orderId = getUrlParaValue(location.href,"orderid");
	var address=$("#address").val();
	var phone=$("#phone").val();
	var remark=$("#remark").val();
	
	clearContent("#address_tip");
	clearContent("#phone_tip");
	clearContent("#remark_tip");
	
	if(!orderId)
	{
		toURL("myOrder.jsp");
	}
	if(address=='')
	{
		showTip("address_tip","* address can't be empty");
		return;
	}
	
	if(!isMobile(phone))
	{
		showTip("phone_tip","* phone number should be 11 digital characters");
		return;
	}
	//使锟斤拷jquery锟斤拷ajax锟斤拷锟斤拷锟斤拷post锟侥凤拷式锟斤拷锟斤拷锟届步锟斤拷锟斤拷执锟叫的讹拷锟斤拷锟角★拷updateOrder锟斤拷
	$.post("UserServlet",{
		action:"updateOrder",
		order:orderId,
		address:address,
		phone:phone,
		remark:remark
	},function(data){
		if(data=='false')
		{
			alert("update order failed");
		}
		if(data=='true')
		{
			toURL("myOrder.jsp");
		}
	});
}
//add item to cart
function addToCart(cakeid)
{
	var quantity=$("#q"+cakeid).val();
	
		if(quantity)
		{
			if(!isNum(quantity))
			{
				alert("invalid quantity");
				$("#q"+cakeid).val("");
				$("#q"+cakeid).focus();
			}
			else
			{
				quantity=parseInt(quantity);
				$.post("UserServlet",{
					action:"addCart",
					cake:cakeid,
					num:quantity
				},function(data){
					if(data=='false')
					{
						alert("add to cart failed");
					}
					if(data=='true')
					{
						alert("add success,you can go to your shopping cart to check");
					}
				});
			}
			
		}
		else
		{
			alert("please enter the quantity number");
		}
	
}

//add one to the cart quantity
function addQ(cartId)
{
	if(cartId)
	{
		$.post("UserServlet",{
			action:"addQ",
			cart:cartId
		},function(data){
			if(data=='true')
			{
				toURL("shoppingCart.jsp");
			}
		});
	}
}

//minus one to the cart quantity
function minusQ(cartId)
{
	if(cartId)
	{
		$.post("UserServlet",{
			action:"minusQ",
			cart:cartId
		},function(data){
			if(data=='true')
			{
				toURL("shoppingCart.jsp");
			}
		});
	}
}


//order from cart
function orderFromCart(cartId)
{
	if(cartId)
	{
		$.post("UserServlet",{
			action:"orderFromCart",
			cart:cartId
		},function(data){
			if(data=='true')
			{
				toURL("myOrder.jsp");
			}
		});
	}
}

//delete cart
function deleteCart(cartId)
{
	if(cartId)
	{
		$.post("UserServlet",{
			action:"deleteCart",
			cart:cartId
		},function(data){
			if(data=='true')
			{
				toURL("shoppingCart.jsp");
			}
		});
	}
}

//order all from cart
function orderAll()
{
	if(cartId)
	{
		$.post("UserServlet",{
			action:"orderAll",
			cart:cartId
		},function(data){
			if(data=='true')
			{
				toURL("myOrder.jsp");
			}
		});
	}
}

//clear cart
function clearCart()
{
	if(confirm("do you want to clear your cart?"))
	{
		$.post("UserServlet",{
			action:"clearCart"
		},function(data){
			if(data=='true')
			{
				toURL("cakes.jsp");
			}
		});
	}
}

function changeUse(val)
{
	use=val;
	$(".choice1").css("background-color","#FFEEEC");
	$("#use"+val).css("background-color","red");
}

function changeTaste(val)
{
	taste=val;
	$(".choice2").css("background-color","#FFEEEC");
	$("#taste"+val).css("background-color","red");
}

function changeSize(val)
{
	size=val;
	$(".choice3").css("background-color","#FFEEEC");
	$("#size"+val).css("background-color","red");
}

function changeLevel(val)
{
	level=val;
	$(".choice4").css("background-color","#FFEEEC");
	$("#level"+val).css("background-color","red");
	$(".l_q").val("");
}
//diy
function diy()
{
	var address=$("#address").val();
	var phone=$("#phone").val();
	var quantity=$("#quantity").val();
	var remark=$("#remark").val();
	var description=$("#r_description").val();
	var info="";
	var amount=0;
	
	if(use=='')
	{
		alert("please choose the cake 'Suitable for'!");
		return;
	}
	if(taste=='')
	{
		alert("please choose the cake 'Taste'!");
		return;
	}
	if(size=='')
	{
		alert("please choose the cake 'Size'!");
		return;
	}
	
	var val=$(".l_q").val();
	if(val)
	{
		level=val;
	}
	if(level=='')
	{
		alert("please choose the cake 'level'!");
		return;
	}
	
	switch(use)
	{
		case 1:
			info+="Suitable for: wedding festival;";
			break;
		case 2:
			info+="Suitable for: birthday (old);";
			break;
		case 3:
			info+="Suitable for: birthday (child);";
			break;
		case 4:
			info+="Suitable for: Valentines Day;";
			break;
		case 5:
			info+="Suitable for: Dinner party;";
			break;
	}
	amount+=100;
	switch(taste)
	{
		case 1:
			info+="Taste: cheese;";
			break;
		case 2:
			info+="Taste: sponge;";
			break;
		case 3:
			info+="Taste: vanilla cream;";
			break;
		case 4:
			info+="Taste: chocolate;";
			break;
		case 5:
			info+="Taste: schwarzwald;";
			break;
		case 6:
			info+="Taste: swiss chocolate;";
			break;
		case 7:
			info+="Taste: white chocolate;";
			break;
	}
	amount+=100;
	switch(size)
	{
		case 1:
			info+="Size: 6inch;";
			amount+=10;
			break;
		case 2:
			info+="Size: 10inch;";
			amount+=20;
			break;
		case 3:
			info+="Size: 14inch;";
			amount+=30;
			break;
		case 4:
			info+="Size: 18inch;";
			amount+=40;
			break;
		case 5:
			info+="Size: 20inch;";
			amount+=50;
			break;
	}

	info+="Layer: layer"+level+";";
	amount+=level*10;
	var knife1=$("#knife1").val();
	if(knife1)
	{
		info+="knife1: "+knife1+";";
		amount+=knife1*10;
	}
	var knife2=$("#knife2").val();
	if(knife2)
	{
		info+="knife2: "+knife2+";";
		amount+=knife2*20;
	}
	var knife3=$("#knife3").val();
	if(knife3)
	{
		info+="knife3: "+knife3+";";
		amount+=knife3*30;
	}
	var fork1=$("#fork1").val();
	if(fork1)
	{
		info+="fork1: "+fork1+";";
		amount+=fork1*10;
	}
	var fork2=$("#fork2").val();
	if(fork2)
	{
		info+="fork2: "+fork2+";";
		amount+=fork2*15;
	}
	var fork3=$("#fork3").val();
	if(fork3)
	{
		info+="fork3: "+fork3+";";
		amount+=fork3*15;
	}
	var dish1=$("#dish1").val();
	if(dish1)
	{
		info+="dish1: "+dish1+";";
		amount+=dish1*10;
	}
	var dish2=$("#dish2").val();
	if(dish2)
	{
		info+="dish2: "+dish2+";";
		amount+=dish2*20;
	}
	var dish3=$("#dish3").val();
	if(dish3)
	{
		info+="dish3: "+dish3+";";
		amount+=dish3*20;
	}
	if(address=='')
	{
		alert("address can't be empty!");
		return;
	}
	if(phone=='')
	{
		alert("phone can't be empty!");
		return;
	}
	description=info+" "+description;
	amount=amount*quantity;
	//alert(encodeURI(encodeURI("UserServlet?action=diy&address="+address+"&phone="+phone+"&quantity="+quantity+"&remark="+remark+"&description="+description+"&amount="+amount)));
//	document.getElementById('upload_form').action=encodeURI(encodeURI("UserServlet?action=diy&address="+address+"&phone="+phone+"&quantity="+quantity+"&description="+description+"&amount="+amount));
//	document.getElementById('upload_form').action="UserServlet?action=diy&address="+address+"&phone="+phone+"&quantity="+quantity+"&description="+description+"&amount="+amount;
	$('#upload_form').attr("action","UserServlet?action=diy&address="+address+"&phone="+phone+"&quantity="+quantity+"&description="+description+"&amount="+amount);
	$('#path').val($('#pic_path').val());
	$('#upload_form').submit();
}

function reset()
{
	$("[class*='choice']").css("background-color","#FFEEEC");
	use="";
	taste="";
	size="";
	level="";
	$(".l_q").val("");
	$(".t_q").val("");
	$("#phone").val("");
	$("#address").val("");
	$("#quantity").val("1");
	$("#remark").val("");
	$("#pic_path").val("");
	$("#r_description").val("");
}

function showOrderList()
{
	$("#switch1").attr("class","switch_btn1");
	$("#switch2").attr("class","switch_btn2");
	$("#orderList").show();
	$("#diyList").hide();
}

function showDiyList()
{
	$("#switch1").attr("class","switch_btn2");
	$("#switch2").attr("class","switch_btn1");
	$("#orderList").hide();
	$("#diyList").show();
}
//閫氳繃榧犳爣鏇存敼鍒�弶鏁伴噺
function changeQ(id,isAdd)
{
	var q=$("#"+id).val();
	var qS=0;
	
	if(isAdd)
	{
		if(q=='')
		{
			$("#"+id).val("1");
		}
		else
		{
			qS=q*1+1;
			$("#"+id).val(qS);
		}
	}
	else
	{
		if(q=='')
		{
			$("#"+id).val("");
		}
		else
		{
			qS=q*1;
			if(qS==1)
			{
				$("#"+id).val("");
			}
			else
			{
				qS-=1;
				$("#"+id).val(qS);
			}
			
		}
	}
}

function leaveMsg()
{
	var msg=$("#msg").val();
	if(msg=='')
	{
		alert('Please input your message first!');
		return;
	}
	
	$.post("UserServlet",{
		action:"leaveMsg",
		msg:msg
	},function(data){
		if(data=='true')
		{
			toURL("msgBoard.jsp");
		}
		else
		{
			alert('failed');
		}
	});
}

function comment(orderid)
{
	$("#commentCtrl").remove();
	var width=400;
	var height=300;
	var content='';
	var isClose=true;
	var title="Comment";
	$("#fullbg_dialog").remove();
	
	var position=$("#order"+orderid).position();
	
	content+='<div id="commentCtrl" style="width:180px;height:60px;background-color:gray;position:absolute;left:'+event.x+'px;top:'+position.top+'px;">'
	content+='<div><textarea id="comment" style="margin-top:10px;width:150px;"></textarea></div>';
	content+='<div><input type="button" value="submit" onclick="commitComment('+orderid+');"/><input type="button" value="cancel" onclick="$(\'#commentCtrl\').remove();" /></div>';
	content+='</div>';
	
	$("body").append(content);
	//showCover(width,height,isClose,content,title);
}

function commitComment(orderId)
{
	
	var comment=$("#comment").val();
	
	if(comment=='')
	{
		alert('Please input your comment!');
		return;
	}
	$.post("UserServlet",{
		action:"comment",
		comment:comment,
		order:orderId
	},function(data){
		if(data=='true')
		{
			alert("comment success");
		}
		else
		{
			alert('comment failed');
		}
		$("#commentCtrl").remove();
	});
	
}

function deleteComment(commentId)
{
	if(confirm("do you really want delete this comment?"))
	{
		$.post("UserServlet",{
			action:"deleteComment",
			comment:commentId
		},function(data){
			if(data=='true')
			{
				toURL(location.href);
			}
			else
			{
				alert('delete comment failed');
			}
		});
	}
}

function deleteCake(cakeId)
{
	$.post("UserServlet",{
		action:"deleteCake",
		cake:cakeId
	},function(data){
		if(data=='true')
		{
			toURL(location.href);
		}
		else
		{
			alert('delete cake failed');
		}
	});
}

function addCake(id)
{
	var name=$("#name").val();
	var price=$("#price").val();
	var categoryId=$("#category").getSelectedValue();
	var categoryName=$("#category").getSelectedText();
	var valid=$("#valid").getSelectedValue();
	var remark=$("#remark").val();
	var picture=$('#pic_path2').val();
	
	if(name=='')
	{
		alert('please input name');
		$("#name").focus();
		return;
	}
	if(price=='')
	{
		alert('please input price');
		$("#price").focus();
		return;
	}
	
	if(id)
	{
		$('#upload_form').attr("action","UserServlet?action=editCake&id="+id+"&name="+name+"&price="+price+"&categoryName="+categoryName+"&valid="+valid+"&remark="+remark);
	}
	else
	{
		$('#upload_form').attr("action","UserServlet?action=addCake&name="+name+"&price="+price+"&categoryId="+categoryId+"&categoryName="+categoryName+"&valid="+valid+"&remark="+remark);
	}
	
	$('#path').val(picture);
	$('#upload_form').submit();
}

function updatePicture(id)
{
	if(id=='')
	{
		toURL('myOrder.jsp');
	}
	
	var picture=$('#pic_path').val();
	
	if(picture=='')
	{
		alert('please choose picture');
		return;
	}
	
	$('#upload_form').attr("action","UserServlet?action=updatePicture&id="+id);
	$('#upload_form').submit();
}